package com.hoshiicloud.vericenter.exceptionHandler;

import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.vericenter.result.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;

@RestControllerAdvice
public class CommonExceptionHandler {
	private final static Logger logger = LoggerFactory.getLogger(CommonExceptionHandler.class);

	/**
	 * 拦截Exception类的异常
	 * 
	 * @param e
	 * @return
	 */
	@ExceptionHandler(Exception.class)
	public Response exceptionHandler(Exception e) {
		e.printStackTrace();
		logger.error("系统异常：" + e.getMessage());
		return Response.failed("系统异常：" + e.getMessage());
	}

	@ExceptionHandler(ServiceException.class)
	public Response serviceExceptionHandler(ServiceException e) {
		e.printStackTrace();
		logger.error("业务异常：" + e.getMessage());
		return Response.failed(e.getMessage());
	}

	@ExceptionHandler(ConstraintViolationException.class)
	public Response constraintViolationExceptionExceptionHandler(ConstraintViolationException e) {
		e.printStackTrace();
		logger.error("参数错误：" + e.getMessage());
		return Response.failed(e.getMessage());
	}
}
